package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.pdf.lib.model.IPdf2ImgListener;
import com.stark.pdf.lib.model.IPdfCreateListener;
import com.stark.pdf.lib.model.PdfUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityImgDetailBinding;
import flc.ast.dialog.ProgressDialog;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.RxUtil;

public class ImgDetailActivity extends BaseAc<ActivityImgDetailBinding> {

    public static String imgPath;
    private Dialog myRenameDialog;
    private EditText editText;
    private ProgressDialog mProgressDialog;


    @Override
    protected int onCreate() {
        return R.layout.activity_img_detail;
    }

    @Override
    protected void initView() {
        mDataBinding.ivImgDetailBack.setOnClickListener(v -> onBackPressed());

        Glide.with(mContext).load(imgPath).into(mDataBinding.ivImgDetailImg);

        mDataBinding.tvRename.setOnClickListener(this);
        mDataBinding.tvPdf.setOnClickListener(this);
        mDataBinding.tvShare.setOnClickListener(this);
        mDataBinding.tvEdit.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);

        mProgressDialog = new ProgressDialog(mContext);
        //重命名弹框
        renameDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvRename://重命名
                myRenameDialog.show();
                break;
            //重命名弹框
            case R.id.ivDialogRenameConfirm:
                String name = editText.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    ToastUtils.showShort(getText(R.string.please_input_video_name));
                    return;
                }
                FileUtils.rename(imgPath, name + "." + FileUtils.getFileExtension(imgPath));
                myRenameDialog.dismiss();
                editText.setText("");
                mDataBinding.tvImgTitle.setText(name + "." + FileUtils.getFileExtension(imgPath));
                break;
            case R.id.ivDialogRenameCancel:
                editText.setText("");
                myRenameDialog.dismiss();
                break;
            case R.id.tvPdf://转PDF
                img2Pdf();
                break;
            case R.id.tvShare://分享
                share();
                break;
            case R.id.tvEdit://编辑
                SingleResultActivity.start(ImgDetailActivity.this, ImageUtils.getBitmap(imgPath), true);
                break;
            case R.id.tvDelete://删除
                delete();
                break;
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvImgTitle.setText(FileUtils.getFileName(imgPath));
    }

    private void delete() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        FileUtils.delete(imgPath);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.delete_suc));
                        finish();
                    }
                });
            }
        }, 1000);
    }

    private void share() {
        IntentUtil.shareFile(mContext, imgPath);
    }

    private void img2Pdf() {
        mProgressDialog.show();
        mProgressDialog.sbProgress.setMax(100);
        mProgressDialog.sbProgress.setProgress(0);
        List<String> list = new ArrayList<>();
        list.add(imgPath);
        PdfUtil.img2Pdf(list, new IPdfCreateListener() {
            @Override
            public void onStart() {
                Random random = new Random();
                int progress = random.nextInt(51);
                mProgressDialog.sbProgress.setProgress(progress);
            }

            @Override
            public void onEnd(boolean success, String path) {
                dismissDialog();
                mProgressDialog.sbProgress.setProgress(100);
                mProgressDialog.sbProgress.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mProgressDialog.dismiss();
                        if (!TextUtils.isEmpty(path)) {
//                            String descPath = PathUtils.getRootPathExternalFirst() + "/" + FileUtils.getFileName(path);
//                            FileUtils.copy(path, descPath);
                            //保存到最近文件
                            String filePath = FileUtil.generateFilePath(Content.myRecent, ".pdf");
                            FileUtils.copy(path, filePath);
                            ToastUtils.showShort(getString(R.string.save_success));
                        } else {
                            ToastUtils.showShort(R.string.change_failure_text);
                        }
                    }
                }, 800);
            }
        });
    }

    //重命名弹框
    private void renameDialog() {
        myRenameDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_rename, null);
        myRenameDialog.setContentView(inflate);
        myRenameDialog.setCancelable(true);
        Window dialogWindow = myRenameDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView cancel = inflate.findViewById(R.id.ivDialogRenameCancel);
        ImageView comp = inflate.findViewById(R.id.ivDialogRenameConfirm);
        editText = inflate.findViewById(R.id.etDialogFormatRename);
        cancel.setOnClickListener(this);
        comp.setOnClickListener(this);
    }

}